/*  --------------------------- (C) COPYRIGHT 2020 Fortiortech ShenZhen -----------------------------
    File Name      : MotorSpeedFunction.c
    Author         : Fortiortech  Appliction Team
    Version        : V1.0
    Date           : 2020-04-10
    Description    : This file contains .C file function used for Motor Control.
    ----------------------------------------------------------------------------------------------------
                                       All Rights Reserved
    ------------------------------------------------------------------------------------------------- */
#include <MyProject.h>

CurrentOffset xdata mcCurOffset;

/*  -------------------------------------------------------------------------------------------------
    Function Name  : Motor_Ready
    Description    : 电机等待状态
    Date           : 2021-01-18
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void Motor_Ready(void)
{
    if (McStaSet.SetFlag.CalibFlag == 0)
    {
        McStaSet.SetFlag.CalibFlag = 1;
        ClrBit(DRV_CR, FOCEN); //该模块关闭FOC模块
        ClrBit(DRV_CR, DRVEN); //该操作会停止DRV内部的计数，关闭载波中断
        MOE = 0;
        SetBit(ADC_MASK, CH4EN | CH2EN | CH1EN | CH0EN | CH7EN);
        mcCurOffset.OffsetFlag = 0;
        mcCurOffset.OffsetCount = 0;            //偏置电压采集计数
        /*外部控制环*/
        memset(&mcFocCtrl, 0, sizeof(FOCCTRL)); // mcFocCtrl变量清零
        /*速度环响应参数初始化*/
        // memset(&MotorSpeed, 0, sizeof(MCRAMP)); // mcSpeedRamp变量清零
    }
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : Motor_Init
    Description    : 电机变量初始化
    Date           : 2021-01-18
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void Motor_Init(void)
{
    ClrBit(ADC_MASK, CH4EN | CH1EN | CH0EN); // 关闭软件电流采样的ADC  FOC模块会自动根据采样模式进行采样，在初始化开启这三个通道是为了采集偏置电压
    VariablesPreInit();                      // 电机相关变量初始化
    ControlLoopInit();                       // 环路控制变量初始化
    SetBit(DRV_CR, DRVEN);                   // 使能DRV内部的计数，开启载波中断
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : Motor_Init
    Description    : PCB桥驱测试
    Date           : 2021-01-18
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void Motor_Charge(void)
{
    if (McStaSet.SetFlag.ChargeSetFlag == 0)
    {
        McStaSet.SetFlag.ChargeSetFlag = 1;
        ClrBit(DRV_CR, OCS);    // 比较值来源
        DRV_DR = 0.3 * DRV_ARR; // 下桥臂 duty
        /*
            DRV_CTL：PWM来源选择
            OCS = 0, DRV_COMR
            OCS = 1, FOC/SVPWM/SPWM
        */
        DRV_CMR &= 0xFFc0;
        MOE = 1;
    }
    
    DRV_CMR |= 0X3F;
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : Motor_Start
    Description    : 电机启动函数
    Date           : 2021-01-18
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void Motor_Start(void)
{
    if (BLDC.BLDCSetFlag == 0)
    {
        ClrBit(DRV_CR, DRVEN);   // 关闭DRV
		CMP0_Open_Init();        // 启动强拖状态下CMP0~2的初始化
        TIM1_BLS_Init();         // TIM1的CMP模式初始化        
        DRV_DR = MIN_START_Duty; // 启动时的最小DUTY
        BLDC.BLDCSetFlag = 1;    // BLDC启动时
        SetBit(DRV_CR, DRVEN);   // 使能DRV
        TIM1_CR4 = 1;            // 强拖启动的第一拍,***注意***：DRV关闭后写TIM1_CR4，不能触发TIM1对DRV的接管，必须在DRV使能以后写TIM1_CR4才能触发状态接管
        MOE = 1;                 // 使能输出
    }
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : Motor_TailWind
    Description    : 顺逆风处理函数
    Date           : 2021-01-18
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void Motor_TailWind(void)
{
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : Motor_Run
    Description    : 电机停止函数
    Date           : 2021-01-18
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void Motor_Run(void)
{
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : Motor_Stop
    Description    : 电机停止函数
    Date           : 2021-01-18
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void Motor_Stop(void)
{
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : Motor_Brake
    Description    : 电机刹车函数
    Date           : 2021-01-18
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void Motor_Brake(void)
{
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : Motor_Fault
    Description    : 电机故障处理函数
    Date           : 2021-01-18
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void Motor_Fault(void)
{
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : MotorcontrolInit
    Description    : 控制变量初始化清零,包括保护参数的初始化、电机状态初始化
    Date           : 2021-01-18
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void MotorcontrolInit(void)
{
    /* -----保护参数初始化----- */
    memset(&mcFaultDect, 0, sizeof(FaultVarible)); // FaultVarible变量清零
    /*保护次数初始化*/
//    memset(&mcProtectTime, 0, sizeof(ProtectVarible)); // ProtectVarible保护次数清零
    /*启停测试参数初始化*/
//    memset(&ONOFFTest, 0, sizeof(ONVarible));

    /*电机状态机时序变量*/
    McStaSet.SetMode = 0;

    /*外部控制环*/
    memset(&mcFocCtrl, 0, sizeof(FOCCTRL)); // mcFocCtrl变量清零
    /*电流偏置校准变量初始化*/
    memset(&mcCurOffset, 0, sizeof(CurrentOffset)); // mcCurOffset变量清零
    mcCurOffset.IuOffsetSum = 16383;
    mcCurOffset.IvOffsetSum = 16383;
    mcCurOffset.Iw_busOffsetSum = 16383;
    /*速度环响应参数初始化*/
    memset(&MotorSpeed, 0, sizeof(MCRAMP)); // mcSpeedRamp变量清零
		
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : VariablesPreInit
    Description    : 初始化电机参数
    Date           : 2020-04-10
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void VariablesPreInit(void)
{
    mcFaultSource = 0;
    /* -----保护参数初始化----- */
    memset(&mcFaultDect, 0, sizeof(FaultVarible)); // FaultVarible变量清零
    /* -----外部控制环参数初始化----- */
    memset(&mcFocCtrl, 0, sizeof(FOCCTRL)); // mcFocCtrl变量清零


    /*****电机状态机时序变量***********/
    McStaSet.SetMode = 0x01; //电流校准标志位置1，其它置0

    mcFocCtrl.State_Count = 0;
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : GetCurrentOffset
    Description    : 上电时，先对硬件电路的电流进行采集，写入对应的校准寄存器中。
                     调试时，需观察mcCurOffset结构体中对应变量是否在范围内。采集结束后，OffsetFlag置1。
    Date           : 2020-04-10
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void GetCurrentOffset(void)
{
    if (!mcCurOffset.OffsetFlag)
    {
        SetBit(ADC_CR, ADCBSY); // 使能ADC
        
        while (ReadBit(ADC_CR, ADCBSY))
            ;
            
        mcCurOffset.Iw_busOffsetSum += ((ADC2_DR & 0x7ff8));
        mcCurOffset.Iw_busOffset = mcCurOffset.Iw_busOffsetSum >> 4;
        mcCurOffset.Iw_busOffsetSum -= mcCurOffset.Iw_busOffset;
        mcCurOffset.OffsetCount++;
        
        if (mcCurOffset.OffsetCount > Calib_Time)
        {
            mcCurOffset.OffsetFlag = 1;
        }
    }
}

